package com.boss.salary.dao.impl;

import com.boss.framework.dao.BaseDaoImpl;
import com.boss.framework.dto.PaginationDTO;
import com.boss.salary.constant.SystemDictConstant;
import com.boss.salary.constant.TableName;
import com.boss.salary.dao.IAgencyInfoAuditDao;
import com.boss.salary.entity.AgencyExt;
import com.boss.salary.entity.AgencyInfo;
import com.boss.sys.utils.SysUtils;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.stereotype.Repository;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Repository
public class AgencyInfoAuditDaoImpl extends BaseDaoImpl implements IAgencyInfoAuditDao {

    @Override
    public List<AgencyInfo> queryAgencyInfoPage(PaginationDTO paginationDTO, Map<String, Object> queryParam, List<String> childrenId) {
        List<Object> paramList = new ArrayList<>();
        StringBuilder sb = new StringBuilder();
        sb.append(" select ")
                .append(" a.id, a.biz_key, a.create_time, a.update_time, a.agency_name, a.mof_div_code,")
                .append("a.location, a.parent_id, a.level_no, a.is_leaf, a.agency_code, a.agency_abbreviation,")
                .append("a.unifsoc_cred_code, a.agency_type, a.agency_state, ");
        sb.append("b.agency_id, b.agency_address, b.zip, b.agency_adm_level_code,")
                .append("b.adm_dep_type_code, b.agency_bank_name, b.agency_bank_cardno, b.central_agency_code,")
                .append("b.agency_leader, b.is_uni_sala, b.remarks,b.has_file,b.fund_sup_code,")
                .append("b.capital_type, b.effective_year, b.verify_state, b.message ");
        sb.append(" from ").append(TableName.AGENCY_INFO).append(" a ");
        sb.append(" left join ").append(TableName.AGENCY_EXT).append(" b on a.id = b.agency_id and b.is_deleted = 0 ");
        sb.append(" where a.is_deleted = 0 ");
        sb.append(" and ").append(SysUtils.inSql("a.id", childrenId));
        if(!CollectionUtils.isEmpty(queryParam)){
            if(!StringUtils.isEmpty(queryParam.get("flag"))){
                sb.append(" and b.verify_state = ? ");
                String flag = String.valueOf(queryParam.get("flag"));
                if(SystemDictConstant.IS_SUBMIT.equals(flag)){
                    paramList.add(SystemDictConstant.IS_SUBMIT);
                }else{
                    paramList.add(SystemDictConstant.IS_AUDIT);
                }
            }
        }
        sb.append(" order by a.create_time ");
        String sql = boxUpSql(sb.toString());
        Object[] params = boxUpParam(paramList.toArray(), paginationDTO);
        return getjdbcTemplate().query(sql, new BeanPropertyRowMapper<>(AgencyInfo.class), params);
    }

    @Override
    public int queryAgencyInfo(Map<String, Object> queryParam, List<String> childrenId) {
        List<Object> paramList = new ArrayList<>();
        StringBuilder sb = new StringBuilder();
        sb.append(" select ").append(" count(*) ");
        sb.append(" from ").append(TableName.AGENCY_INFO).append(" a ");
        sb.append(" left join ").append(TableName.AGENCY_EXT).append(" b on a.id = b.agency_id and b.is_deleted = 0 ");
        sb.append(" where a.is_deleted = 0 ");
        sb.append(" and ").append(SysUtils.inSql("a.id", childrenId));
        if(!CollectionUtils.isEmpty(queryParam)){
            if(!StringUtils.isEmpty(queryParam.get("flag"))){
                sb.append(" and b.verify_state = ? ");
                String flag = String.valueOf(queryParam.get("flag"));
                if(SystemDictConstant.IS_SUBMIT.equals(flag)){
                    paramList.add(SystemDictConstant.IS_SUBMIT);
                }else{
                    paramList.add(SystemDictConstant.IS_AUDIT);
                }
            }
        }
        return getjdbcTemplate().queryForObject(sb.toString(), paramList.toArray(), Integer.class);
    }

    @Override
    public void updateAgencyInfoState(AgencyExt state) {
        List<Object> paramList = new ArrayList<>();
        StringBuilder sb = new StringBuilder();
        sb.append(" update ").append(TableName.AGENCY_EXT).append(" set is_deleted = 0 , update_time = ?");
        paramList.add(state.getUpdateTime());
        String verifyState = state.getVerifyState();
        if(!StringUtils.isEmpty(verifyState)){
            sb.append(", verify_state = ? ");
            paramList.add(verifyState);
        }
        sb.append(", message = ? ");
        paramList.add(state.getMessage());

        sb.append(" where is_deleted = 0 ").append(" and agency_id = ? ");
        paramList.add(state.getAgencyId());
        this.getjdbcTemplate().update(sb.toString(), paramList.toArray());
    }

}
